#include "delay.h"
#include "sys.h"
#include "led.h"
#include "key.h"
#include "adc.h"
#include "iwdg.h"
#include "pwm.h"
#include "Whole_Motor_Parameters.h"
#include "GPIO.h"
#include "pwr_pvd.h"
#include "my_uart.h"
#include "for_test_message.h"
#include "PWM_control.h"
#include "time_manager.h"
#include "motor_status.h"
#include "BLDC_change.h"
extern uint32_t adc1_times;
extern pole_status pole_status_set;
uint32_t Bemf_A_number[45];
uint16_t u16ADC1[10];
uint16_t u16ADC2[10];
uint32_t u16ADC1_16in1_adc[10];
uint32_t u16ADC1_2in1_adc[10];
uint32_t adc_5in1_adc1_1[6];
u16 i_high_value = 0, i_low_value = 0;

void ADC1_IRQHandler(void)
{
    if (adc1_times++ > 10000000)
    {
        adc1_times = 0;
    }
    /* Enable ADC Intterupt */
    // ADC1->ADCR |= ADC_IT_EOC; //
    // ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
    /* Enable HW Trigger */
    // ADC1->ADCR |= ADCR_EXTTRIG_Set; //
    /* Clear ADC Flag of Conversion */
    // ADC1->ADSTA |= ADC_IT_EOC; //
    ADC_ClearFlag(ADC1, ADC_IT_EOC);
    // ADC2->ADSTA |= ADC_IT_EOC; //
    ADC_ClearFlag(ADC2, ADC_IT_EOC);
    ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);
    ADC_ITConfig(ADC2, ADC_IT_EOC, DISABLE);
    /* Disable HW Trigger */
    // ADC1->ADCR &= ADCR_EXTTRIG_Reset; //
    ADC_ExternalTrigConvCmd(ADC1, DISABLE); //检查中断点1
    // ADC2->ADCR &= ADCR_EXTTRIG_Reset; //
    ADC_ExternalTrigConvCmd(ADC2, DISABLE);
    u16ADC1[0] = ADC1->ADDR0;
    u16ADC1[1] = ADC1->ADDR1;
    u16ADC1[2] = ADC1->ADDR2;
    u16ADC1_16in1_adc[0] = u16ADC1_16in1_adc[0] * 15 / 16 + u16ADC1[0];
    u16ADC1_16in1_adc[1] = u16ADC1_16in1_adc[1] * 15 / 16 + u16ADC1[1];
    u16ADC1_16in1_adc[2] = u16ADC1_16in1_adc[2] * 15 / 16 + u16ADC1[2];
    u16ADC1_2in1_adc[0] = u16ADC1_2in1_adc[0] * 1 / 2 + u16ADC1[0];
    u16ADC1_2in1_adc[1] = u16ADC1_2in1_adc[1] * 1 / 2 + u16ADC1[1];
    u16ADC1_2in1_adc[2] = u16ADC1_2in1_adc[2] * 1 / 2 + u16ADC1[2];
    if (pole_status_set.u_pole_status == 1)
    {
        set_u_max_value(u16ADC1_16in1_adc[0]);
        clear_min_flag();
    }
    else if (pole_status_set.v_pole_status == 1)
    {
        set_v_max_value(u16ADC1_16in1_adc[1]);
        clear_min_flag();
    }
    else if (pole_status_set.w_pole_status == 1)
    {
        set_w_max_value(u16ADC1_16in1_adc[2]);
        clear_min_flag();
    }
    else if (pole_status_set.u_pole_status == 4)
    {
        set_u_min_value(u16ADC1_16in1_adc[0]);
        clear_max_flag();
    }
    else if (pole_status_set.v_pole_status == 4)
    {
        set_v_min_value(u16ADC1_16in1_adc[1]);
        clear_max_flag();
    }
    else if (pole_status_set.w_pole_status == 4)
    {
        set_w_min_value(u16ADC1_16in1_adc[2]);
        clear_max_flag();
    }
    motor_reserving_jugement();
    TIM1->CCR4 = 0;
    TIM1->CCR5 = 0;
    // adc_flag_set_and_check_status();
    ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
    ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
    ADC_ExternalTrigConvCmd(ADC1, ENABLE);
    // ADC1_Channel_Setup_to_2Phase_Current_Only(); // change ADC1 channels to phase current measurement
    ADC_ExternalTrigConvCmd(ADC2, ENABLE);
    // ADC2_Channel_Setup_to_2Phase_Current_Only(); // change ADC2 channels to phase current measurement
    ADC1_Channel_Setup__BEMF_ABC();
}

int fputc(int ch, FILE *f)
{
    while ((UART1->CSR & UART_IT_TXIEN) == 0)
        ; //循环发送,直到发送完毕
    UART1->TDR = (ch & (uint16_t)0x00FF);
    return ch;
}
int main(void)
{
    systick_init();
    LED_Init();
    _my_uart_init();
    delay_ms(200);
    ADC1_Initial();
    ADC1_Channel_Setup__BEMF_ABC();
    ADC2_Initial();
    ADC2_Channel_Setup_Without_Phase_Current();
    TIM1_PWM_Init(PWM_PERIOD, 0, DEAD_TIME_SETUP); // PWM initialize
    TIM3_PWM_Init(PWM_PERIOD, 0);
    motor_status_set(0, 2, 4);
    BLDC_motor_modulation_value_set(40);
    Pull_Down_all_PWM();
    redefine_all_PWM_Pin();
    set_speed(0);
    set_motor_start();
    // Pull_Down_all_PWM();
    while (1)
    {
        System_Time_Management();
        // printf("adc1_times:%d", adc1_times);
    }
}
